<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件

/**
 * 处理成功
 * @param type $data
 * @param type $msg
 * @return type
 */
function success($data = [], $msg = '成功') {
    return ['code' => 0, 'msg' => $msg, 'data' => $data];
}

/**
 * 处理失败
 * @param type $msg
 * @param type $code
 * @param type $data
 * @return type
 */
function fail($msg = '错误', $code = 1, $data = []) {
    return ['code' => $code, 'msg' => $msg, 'data' => $data];
}

/**
 * 产生随机字符串
 * @param    int        $length  输出长度
 * @param    string     $chars   可选的 ，默认为 0123456789
 * @return   string     字符串
 */
function random($length, $chars = '0123456789') {
    $hash = '';
    $max = strlen($chars) - 1;
    mt_srand();
    for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

/**
 * 生成随机字符串
 * @param string $lenth 长度
 * @return string 字符串
 */
function create_randomstr($lenth = 6) {
    return random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
}

/**
 * 后台input[text]
 * @param type $key
 * @param type $title
 * @param type $value
 * @param type $class
 * @param type $required
 * @param type $col
 * @return type
 */
function adminFormInputText($key, $title, $value = '', $required = 0, $placeholder = '', $staticText = '', $class = '', $col = 6) {
    $_placeholder = $placeholder ? $placeholder : $title;
    $_required = $required ? 'required' : '';
    if ($staticText) {
        $staticHtml = '<div class="col-sm-3 help-block" id="static_{$key}">' . $staticText . '</div>';
        $col = 6;
    } else {
        $staticHtml = '';
    }
    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                    <input type="text" class="form-control {$class}" id="form_{$key}" name="{$key}" value="{$value}" placeholder="{$_placeholder}" {$_required} />
                </div>
                {$staticHtml}
            </div>
HTML;
    return $htmlStr;
}

/**
 * input:number 表单
 * @param type $key
 * @param type $title 
 * @param type $value
 * @param type $step
 * @param type $staticText
 * @param type $class
 * @param int $col 内容宽度
 * @return type
 */
function adminFormInputNumber($key, $title, $value = '', $step = '', $staticText = '', $class = '', $col = 6) {
    $_step = $step ? "step='{$step}'" : '';
    if ($staticText) {
        $staticHtml = '<div class="col-sm-3 help-block" id="static_{$key}">' . $staticText . '</div>';
        $col = 6;
    } else {
        $staticHtml = '';
    }
    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                    <input type="number" {$_step} class="form-control {$class}" id="form_{$key}" name="{$key}" value="{$value}"   />
                </div>
                {$staticHtml}
            </div>
HTML;
    return $htmlStr;
}

/**
 * textarea 表单
 * @param type $key
 * @param type $title
 * @param type $value
 * @param type $required
 * @param type $class
 * @param type $rows
 * @param type $col
 * @return type
 */
function adminFormTextarea($key, $title, $value = '', $required = '', $class = '', $rows = 5, $col = 6) {
    $_required = $required ? 'required' : '';
    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                    <textarea class="form-control {$class}" id="form_{$key}" name="{$key}" rows="{$rows}" {$_required}>{$value}</textarea>
                </div>
            </div>
HTML;
    return $htmlStr;
}

/**
 * select 表单
 * @param type $key
 * @param type $title
 * @param type $data
 * @param type $value
 * @param type $required
 * @param int $col
 * @param type $staticText
 * @return type
 */
function adminFormSelect($key, $title, $data, $value = '', $required = '', $col = 3, $staticText = '') {
    $_required = $required ? 'required' : '';
    $selectHtml = "<select id='form_{$key}' name='{$key}' class='form-control' {$_required}><option value=''>{$title}</option>";
    foreach ($data as $k => $v) {
        if ($k == $value) {
            $selectHtml .= "<option value='{$k}' selected>{$v}</option>";
        } else {
            $selectHtml .= "<option value='{$k}'>{$v}</option>";
        }
    }
    $selectHtml .= "</select>";

    if ($staticText) {
        $staticHtml = '<div class="col-sm-3 help-block" id="static_{$key}">' . $staticText . '</div>';
        $col = 3;
    } else {
        $staticHtml = '';
    }

    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                {$selectHtml}
                </div>
                {$staticHtml}
            </div>
HTML;
    return $htmlStr;
}

/**
 * input:checkbox 表单
 * @param type $key
 * @param type $title
 * @param type $data
 * @param type $value
 * @param int $col
 * @param type $staticText
 * @return type
 */
function adminFormCheckbox($key, $title, $data, $value = [], $col = 10, $staticText = '') {
    $checkboxHtml = "";
    if (!is_array($value)) {
        $value = [$value];
    }
    foreach ($data as $k => $v) {
        if (in_array($k, $value)) {
            $checkboxHtml .= "<label class='control-label'><input type='checkbox' class='minimal' name='{$key}[]' value='{$k}' checked > {$v}</label>&nbsp;&nbsp;&nbsp;&nbsp;";
        } else {
            $checkboxHtml .= "<label class='control-label'><input type='checkbox' class='minimal' name='{$key}[]' value='{$k}' > {$v}</label>&nbsp;&nbsp;&nbsp;&nbsp;";
        }
    }

    if ($staticText) {
        $staticHtml = '<div class="col-sm-3 help-block" id="static_{$key}">' . $staticText . '</div>';
        $col = 7;
    } else {
        $staticHtml = '';
    }

    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                {$checkboxHtml}
                </div>
                {$staticHtml}
            </div>
HTML;
    return $htmlStr;
}

/**
 * input:radio 表单
 * @param type $key
 * @param type $title
 * @param type $data
 * @param type $value
 * @param int $col
 * @param type $staticText
 * @return type
 */
function adminFormRadio($key, $title, $data, $value = '', $col = 10, $staticText = '') {
    $checkboxHtml = "";
    foreach ($data as $k => $v) {
        if ($k == $value) {
            $checkboxHtml .= "<label class='control-label'><input type='radio' class='minimal' name='{$key}' value='{$k}' checked > {$v}</label>&nbsp;&nbsp;&nbsp;&nbsp;";
        } else {
            $checkboxHtml .= "<label class='control-label'><input type='radio' class='minimal' name='{$key}' value='{$k}' > {$v}</label>&nbsp;&nbsp;&nbsp;&nbsp;";
        }
    }

    if ($staticText) {
        $staticHtml = '<div class="col-sm-3 help-block" id="static_{$key}">' . $staticText . '</div>';
        $col = 7;
    } else {
        $staticHtml = '';
    }

    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-{$col}">
                {$checkboxHtml}
                </div>
                {$staticHtml}
            </div>
HTML;
    return $htmlStr;
}

/**
 * input:file 表单
 * @param type $key
 * @param type $title
 * @param type $value
 * @param type $required
 * @return type
 */
function adminFormInputFile($key, $title, $value = '', $required = 0) {

    $_required = $required ? 'required' : '';
    $htmlStr = <<<HTML
            <div class="form-group">
                <label for="form_{$key}" class="col-sm-2 control-label">{$title}：</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" id="form_{$key}" name="{$key}" value="{$value}" placeholder="{$title}" {$_required} />
                </div>
                <div class="col-sm-3">
                    <input type="file" class="" onchange="upAdminFile(this,'form_{$key}')" />
                </div>
                <div class="col-sm-1">
                    <input type="button" class="form-control" onclick="previewAdminFile('form_{$key}')" value="预览"/>
                </div>
            </div>
HTML;
    return $htmlStr;
}

/**
 * 多文件上传
 * @param type $data
 * @param type $key
 * @param type $pathKey
 * @param type $titleKey
 * @param type $title
 * @return type
 */
function adminFormMultiFile($data = [], $key = 'multi_file', $pathKey = "path", $titleKey = "title", $title = '多图上传') {
    $oldHtml = '';
    if ($data) {
        foreach ($data as $row) {
            $oldHtml .= "<div class='row'>
		<div class='col-sm-1'>
			<input type='text'  class='form-control' name='{$key}[order][]' value=''   />
		</div>
		<div class='col-sm-5 formMulti_path'>
			<input type='text'  class='form-control' name='{$key}[{$pathKey}][]' value='{$row['path']}'   />
		</div>
		<div class='col-sm-4'>
			<input type='text'  class='form-control' name='{$key}[{$titleKey}][]' value='{$row['title']}'   />
		</div>
		<div class='col-sm-2'>
			<i class='fa fa-fw fa-file' onclick='multiSelectFile(\"formMultiBox_{$key}\",this,\"formMultiFile_{$key}\")'></i>
			<i class='fa fa-fw fa-eye' onclick='previewAdminMultiFile(this)'></i>
			<i class='fa fa-fw fa-close' onclick='delAdminMultiFile(this)'></i>
		</div>
	</div>";
        }
    }
    $htmlStr = <<<HTML
        <div class="form-group">
            <label class="col-sm-2 control-label">图片附件</label>

            <div class="col-sm-10 multi_file" id="formMultiBox_{$key}">
                <div class="row">
                    <label class="col-sm-1 control-label text-left">排序</label>
                    <label class="col-sm-5 control-label">文件地址</label>
                    <label class="col-sm-4 control-label">文件描述</label>
                    <label class="col-sm-2 control-label">文件操作</label>
                </div>
                {$oldHtml}
                <div class="row">
                    <div class="col-sm-1">
                        <input type="text"  class="form-control" name="{$key}[order][]" value=""   />
                    </div>
                    <div class="col-sm-5 formMulti_path">
                        <input type="text"  class="form-control" name="{$key}[{$pathKey}][]" value=""   />
                    </div>
                    <div class="col-sm-4">
                        <input type="text"  class="form-control" name="{$key}[{$titleKey}][]" value=""   />
                    </div>
                    <div class="col-sm-2">
                        <i class="fa fa-fw fa-file" onclick="multiSelectFile('formMultiBox_{$key}',this,'formMultiFile_{$key}')"></i>
                        <i class="fa fa-fw fa-eye" onclick="previewAdminMultiFile(this)"></i>
                        <i class="fa fa-fw fa-close" onclick="delAdminMultiFile(this)"></i>
                    </div>
                </div>
                <div class="row" id="formMultiBtn_{$key}">
                    <div class="col-sm-2">
                        <input type="button"  class="form-control"  value="+增加"  onclick="addMultiHtml('formMultiTemplate_{$key}','formMultiBtn_{$key}')" />
                    </div>
                    <div style="display:none;">
                        <input type="file"  id="formMultiFile_{$key}" onchange="multiUploadFile(this,'formMultiBox_{$key}')"  />
                    </div>
                </div>
            </div>
            <script type="text/html" id="formMultiTemplate_{$key}">
                <div class="row">
                    <div class="col-sm-1">
                        <input type="text"  class="form-control" name="{$key}[order][]" value=""   />
                    </div>
                    <div class="col-sm-5 formMulti_path">
                        <input type="text"  class="form-control" name="{$key}[{$pathKey}][]" value=""   />
                    </div>
                    <div class="col-sm-4">
                        <input type="text"  class="form-control" name="{$key}[{$titleKey}][]" value=""   />
                    </div>
                    <div class="col-sm-2">
                        <i class="fa fa-fw fa-file" onclick="multiSelectFile('formMultiBox_{$key}',this,'formMultiFile_{$key}')"></i>
                        <i class="fa fa-fw fa-eye" onclick="previewAdminMultiFile(this)"></i>
                        <i class="fa fa-fw fa-close" onclick="delAdminMultiFile(this)"></i>
                    </div>
                </div>
            </script>
        </div>
HTML;
    return $htmlStr;
}

/**
 * select
 * @param type $key
 * @param type $data
 * @param type $value
 * @param type $title
 * @param type $required
 * @return string
 */
function formSelect($key, $data, $value = '', $title = '', $required = '') {
    $_required = $required ? 'required' : '';
    $optionTitle = $title ? "<option value=''>--选择 {$title}--</option>" : '';

    $selectHtml = "<select id='form_{$key}' name='{$key}' class='form-control' {$_required}>{$optionTitle}";
    foreach ($data as $k => $v) {
        if ($k == $value) {
            $selectHtml .= "<option value='{$k}' selected>{$v}</option>";
        } else {
            $selectHtml .= "<option value='{$k}'>{$v}</option>";
        }
    }
    $selectHtml .= "</select>";
    return $selectHtml;
}
